package cn.enaium.message.config;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.aspectj.weaver.ast.Test;

import java.io.InputStream;


public class JdbcConfig {
    //mapper代理实现自定义接口
    InputStream inputStream= Test.class.getClassLoader().getResourceAsStream("config.xml");
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();

    public SqlSession getSqlSession(){
        return this.sqlSession;
    }
    public void sqlSessionCommit(){
        this.sqlSession.commit();
    }
    public void sqlSessionClose(){
        this.sqlSession.close();
    }



    //获取实现接口的代理对象
//    CollectionRepository collectionRepository = sqlSession.getMapper(cn.enaium.message.repository.CollectionRepository.class);
    //添加  事务有变化要提交
//        Collection collection1 =new Collection(1L, 1L);
//        int result=collectionRepository.save(collection1); //成功就是1
//        sqlSession.commit();
    //查询
//    List<Collection> list = collectionRepository.findAll(1L);
//        for (Collection collection : list) {
//        System.out.println(collection.getId());
//    }


    // 修改
//        Collection collection2=collectionRepository.findByuid(1L);
//        collection2.setUid(1L);
//        collection2.setId(2L);
//        int result1 = collectionRepository.update(collection2);
//        sqlSession.commit();
    //删除
//        int result2 = collectionRepository.deleteByuid(1L);
//        sqlSession.commit();

//        sqlSession.close();
}
